/******************************************************************
 * Created by: GOh Chee Hong                                      
 * Date of Creation: 1/Jan/2009

 * 
 *                                                                
 * Description: This class is in charge of downloading the file
 ******************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace Pbdms.BusinessLogic.Mail
{
    public class FileDownloader
    {
        private int start_block;
        private int no_of_blocks;
        private int end_block;
      
        private string key;
        private string fileLocation;
        private FileStream fileToWrite;
        private Boolean Finished = false;
        public delegate Boolean checkCancel();
        public checkCancel isCancel;
        public Boolean IsFinished()
        {
            return Finished;
        }
        public int TotalBlocks()
        {
            return no_of_blocks;
        }
        public int StartBlock()
        {
            return start_block;
        }
        public int EndBlock()
        {
            return end_block;
        }
        public FileDownloader()
        {

        }
        ~FileDownloader()
        {
            if (fileToWrite != null)
            {
                fileToWrite.Close();
            }
        }
        public FileDownloader(string key, int start_block, int no_of_blocks, string fileLocation)
        {
            this.key = key;
            this.start_block = start_block;
            this.no_of_blocks = no_of_blocks;
       
            this.fileLocation = fileLocation;
        }
        public void download()
        {
            try
            {
                OpenDHTLib.OpenLookUp openLookUp = new Pbdms.OpenDHTLib.OpenLookUp();
                fileToWrite = new System.IO.FileStream(fileLocation, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, FileShare.Write);
                end_block = start_block + no_of_blocks;
                fileToWrite.Position = start_block*1024;
                //calculate the size of the data
                for (int i = start_block; i < end_block; i++)
                {
                    //set position
                    if (isCancel != null)
                    {
                        if (isCancel() == true)
                        {
                            return;
                        }
                    }
                    byte[] data = openLookUp.GetARawValue(key + " Block No:" + i);
                    if (data != null)
                    {
                        fileToWrite.Write(data, 0, data.Length);
                    }
                    else
                    {
                        //error

                        return;
                    }
                }
                Finished = true;
            }
            catch
            {

            }
            finally
            {
                if (fileToWrite != null)
                {
                    fileToWrite.Close();
                }
            }
        }
    }
}
